import mssql
import config
from typing import Dict

def query(sql):
    db = mssql.ZgqMssql(config.get('db'))
    dataList = db.query(sql)
    db.close()
    return dataList

# 获取医疗机构信息
def getHosp():
    list = query('select hosp_id, hosp_nbr, convert(nvarchar(100),name) name, country_code from hosp_base')
    return {
        "code": 200,
        "msg": 'success',
        "data": list[0]
    }
    
# 获取药品单位
def mediUnitList():
    list = query('select medi_unit_id value, convert(nvarchar(1000),name) label from medi_unit where sts = \'A\'')
    return {
        "code": 200,
        "msg": 'success',
        "data": {
            "dataList": list
        }
    }
    
# 用药途径
def mediPassList():
    list = query('select medi_pass value, convert(nvarchar(1000), name) label from medi_pass where sts = \'A\' order by stor_id')
    return {
        "code": 200,
        "msg": 'success',
        "data": {
            "dataList": list
        }
    }
    
# 用药频次
def useTypeList():
    list = query('select use_type_id value, convert(nvarchar(1000),name) label from use_type where sts = \'A\' order by sort_id')
    return {
        "code": 200,
        "msg": 'success',
        "data": {
            "dataList": list
        }
    }

# 诊断编码
def diagList(data: Dict):
    sql = 'select top 10 code value,convert(nvarchar(1000), mc) label from Xsnb_icd10 '
    if data.get('keyword'):
        sql = sql + 'where zxpadd_sts = \'A\' and (mc like \'%' + data.get('keyword') + \
            '%\' or wb like \'%' + data.get('keyword') + \
            '%\' or py like \'%' + data.get('keyword') + '%\')'
    elif data.get('codeList'):
        sql = sql + 'where zxpadd_sts = \'A\' and code in (\'' + '\',\''.join(data.get('codeList')) + '\')'
    else:
        sql = sql + 'where 1 > 1'

    list = query(sql)
    return {
        "code": 200,
        "msg": 'success',
        "data": {
            "dataList": list
        }
    }
    
def getDiagIdByCode(code: str):
    sql = 'SELECT convert(bigint, id) id FROM xsnb_icd10 WHERE code = \'' + code +'\' AND zxpadd_sts = \'A\''
    list = query(sql)
    return list[0]['id']

#支付类型
def chargeCatList():
    list = query('SELECT charge_cat_id value, convert(nvarchar(1000),name) label FROM charge_cat WHERE sts = \'A\' ORDER BY sort_id')
    return {
        "code": 200,
        "msg": 'success',
        "data": {
            "dataList": list
        }
    }
#文件结束
            
            